package com.pxl.springbootinit.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.pxl.springbootinit.common.ErrorCode;
import com.pxl.springbootinit.exception.BusinessException;
import com.pxl.springbootinit.model.dto.task.TaskClaimForm;
import com.pxl.springbootinit.model.dto.task.TaskForm;
import com.pxl.springbootinit.model.entity.Task;
import com.pxl.springbootinit.model.entity.TaskClaim;
import com.pxl.springbootinit.model.entity.User;
import com.pxl.springbootinit.model.enums.ReviewStatusEnum;
import com.pxl.springbootinit.model.enums.TaskStatusEnum;
import com.pxl.springbootinit.model.vo.TaskVO;
import com.pxl.springbootinit.service.TaskClaimService;
import com.pxl.springbootinit.service.TaskService;
import com.pxl.springbootinit.mapper.TaskMapper;
import com.pxl.springbootinit.service.UserService;
import org.springframework.beans.BeanUtils;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import javax.annotation.Resource;
import java.util.List;
import java.util.stream.Collectors;

/**
* @author MADAO
* @description 针对表【task(任务发布表)】的数据库操作Service实现
* @createDate 2024-12-20 11:59:03
*/
@Service
public class TaskServiceImpl extends ServiceImpl<TaskMapper, Task>
    implements TaskService{

    /**
     * 发布任务
     * @param taskForm
     * @return
     */
    @Override
    public Boolean publishTask(TaskForm taskForm) {
        Task task = new Task();
        BeanUtils.copyProperties(taskForm, task);
        task.setStatus(TaskStatusEnum.PENDING.getValue());
        task.setId(null);
        boolean result = this.save(task);
        if (!result){
            throw new BusinessException(ErrorCode.SYSTEM_ERROR,"发布失败，请重试");
        }
        return true;
    }

    /**
     * 追踪任务
     * @param id
     * @return
     */
    @Override
    public TaskVO traceTask(Long id) {
        return getTaskVO(this.getById(id));
    }

    @Override
    public Page<TaskVO> pageTask(TaskForm taskForm) {
        int current = taskForm.getCurrent();
        int pageSize = taskForm.getPageSize();
        QueryWrapper<Task> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("status", TaskStatusEnum.PENDING.getValue());
        queryWrapper.like(taskForm.getKeyword()  != null, "title", taskForm.getKeyword());
        queryWrapper.like(taskForm.getTaskType() != null, "task_type", taskForm.getTaskType());
        queryWrapper.lt(taskForm.getDonationDeadline() != null, "donation_deadline", taskForm.getDonationDeadline());
        queryWrapper.orderByDesc("updated_time");
        Page<Task> taskPage = this.page(new Page<>(current, pageSize),queryWrapper);
        List<TaskVO> collect = taskPage.getRecords().stream().map(task -> getTaskVO(task)).collect(Collectors.toList());
        Page<TaskVO> taskVOPage = new Page<>();
        taskVOPage.setPages(taskPage.getPages());
        taskVOPage.setTotal(taskPage.getTotal());
        taskVOPage.setRecords(collect);
        return taskVOPage;
    }

    @Override
    public TaskVO getTaskVO(Task task){
        TaskVO taskVO = new TaskVO();
        BeanUtil.copyProperties(task, taskVO);
        return taskVO;
    }

    @Override
    public Boolean finishTask(Long id, Long userId) {
        if (id == null ){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"任务ID不能为空");
        }
        Task task = this.getById(id);
        if (task == null){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"任务不存在");
        }
        if (!task.getUserId().equals(userId)){
            throw new BusinessException(ErrorCode.PARAMS_ERROR,"任务不属于该用户");
        }
        task = new Task();
        task.setId(id);
        task.setStatus(TaskStatusEnum.COMPLETED.getValue());
        return this.updateById(task);
    }

    @Override
    public Page<TaskVO> pageAdminTask(TaskForm taskForm) {
        int current = taskForm.getCurrent();
        int pageSize = taskForm.getPageSize();
        QueryWrapper<Task> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq( taskForm.getStatus() != null,"status", taskForm.getStatus());
        queryWrapper.like(taskForm.getTitle()  != null, "title", taskForm.getTitle());
        queryWrapper.like(taskForm.getTaskType() != null, "task_type", taskForm.getTaskType());
        queryWrapper.lt(taskForm.getDonationDeadline() != null, "donation_deadline", taskForm.getDonationDeadline());
        queryWrapper.orderByDesc("updated_time");
        Page<Task> taskPage = this.page(new Page<>(current, pageSize),queryWrapper);
        List<TaskVO> collect = taskPage.getRecords().stream().map(task -> getTaskVO(task)).collect(Collectors.toList());
        Page<TaskVO> taskVOPage = new Page<>();
        taskVOPage.setPages(taskPage.getPages());
        taskVOPage.setTotal(taskPage.getTotal());
        taskVOPage.setRecords(collect);
        return taskVOPage;
    }

    @Override
    public Page<TaskVO> handleReviewPage(TaskForm taskForm, Long userId) {
        int current = taskForm.getCurrent();
        int pageSize = taskForm.getPageSize();
        QueryWrapper<Task> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq( "status", TaskStatusEnum.COMPLETED.getValue());
        queryWrapper.eq("user_id",userId);
        queryWrapper.orderByDesc("updated_time");
        Page<Task> taskPage = this.page(new Page<>(current, pageSize),queryWrapper);
        List<TaskVO> collect = taskPage.getRecords().stream().map(task -> getTaskVO(task)).collect(Collectors.toList());
        Page<TaskVO> taskVOPage = new Page<>();
        taskVOPage.setPages(taskPage.getPages());
        taskVOPage.setTotal(taskPage.getTotal());
        taskVOPage.setRecords(collect);
        return taskVOPage;
    }


}




